﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ZZQFrameWork;

/// <summary>
/// 技能面板属性panel
/// </summary>
public class SkillPanelAttrPanel:MonoBehaviour
{
    /// <summary>
    /// 属性点
    /// </summary>
    private Text attrPointText;

    /// <summary>
    /// 所有的属性item集合
    /// </summary>
    private SkillPanelAttrItem[] attrItems;

    /// <summary>
    /// 当前选择的属性
    /// </summary>
    private SkillPanelAttrItem currentAttrItem;

    /// <summary>
    /// 可用属性点
    /// </summary>
    private int attrPoint;

    public int AttrPoint
    {
        get { return attrPoint; }
        set
        {
            attrPoint = value;
            UpdateShow();
        }
    }

    void Awake()
    {
        attrPointText = transform.GetComponentByPath<Text>("BG/PointBg/Value");
        attrItems = gameObject.GetComponentsInChildren<SkillPanelAttrItem>(true);
    }

    void OnEnable()
    {
        //EventDispatcher.AddListener<int>(Events.RoleEvent.OnRoleLevelUp,OnRoleLevelUp);
        EventDispatcher.AddListener<SkillVO, int>(Events.RoleEvent.OnLearnSkill, ReduceAttrPoint);
        RefreshPanel();
    }

    void OnDisable()
    {
        //EventDispatcher.RemoveListener<int>(Events.RoleEvent.OnRoleLevelUp, OnRoleLevelUp);
        EventDispatcher.RemoveListener<SkillVO, int>(Events.RoleEvent.OnLearnSkill, ReduceAttrPoint);
    }

    void UpdateShow()
    {
        attrPointText.text = attrPoint.ToString();
    }

    /// <summary>
    /// 属性item点击
    /// </summary>
    /// <param name="_listener"></param>
    /// <param name="_args"></param>
    /// <param name="_params"></param>
    private void OnAttrItemClick(GameObject _listener, object _args, object[] _params)
    {
        SkillPanelAttrItem item = _listener.GetComponent<SkillPanelAttrItem>();
        if (currentAttrItem != null)
        {
            if(currentAttrItem==item)return;
            currentAttrItem.SetItemClicked(false);
        }
        currentAttrItem = item;
        currentAttrItem.SetItemClicked(true);
    }

    /// <summary>
    /// 学习技能，扣除1点技能点，也可以增加技能属性，学该技能需要消耗多少点技能点
    /// </summary>
    private void ReduceAttrPoint(SkillVO skill,int point)
    {
        AttrPoint = point;
    }

    void RefreshPanel()
    {
        for (int i = 0; i < attrItems.Length; i++)
        {
            attrItems[i].Data = ModuleManager.Instance.Get<CharacterModule>().mainRole.attrDataList[i];
            EventTriggerListener.Get(attrItems[i].gameObject).SetEventHandle(Enum_TouchEventType.OnClick, OnAttrItemClick);
        }
        AttrPoint = ModuleManager.Instance.Get<CharacterModule>().mainRole.attrPoint;
    }
}
